﻿using AutoMapper;
using MediatR;
using ZGLXM.ErrorCode;
using ZGLXM.Infra.Interface;
using ZGLXM.Infra.Repository;
using ZGLXM.Read.Api.Applicantion.Command.RBAC;

namespace ZGLXM.Read.Api.Applicantion.Handler.RBAC
{
    public class UserByIdCommandHandler : IRequestHandler<UserByIdCommand, ApiResult>
    {
        private readonly IUserRepository userRepository;
        private readonly IMapper mapper;
        private readonly ILogger<UserByIdCommandHandler> logger;
        public UserByIdCommandHandler(IUserRepository userRepository, IMapper mapper, ILogger<UserByIdCommandHandler> logger)
        {
            this.userRepository = userRepository;
            this.mapper = mapper;
            this.logger = logger;
        }

        public async Task<ApiResult> Handle(UserByIdCommand request, CancellationToken cancellationToken)
        {
            var user = await userRepository.GetAsync(x => x.IsDeleted == false && x.UserId == request.UserId);
            if (user == null)
            {
                return ApiResult.Fail("信息不存在");
            }
            return ApiResult.OK(user);
        }
    }
}
